﻿using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Configuration;
using Gadgets;
using System.Security.Cryptography;
using System.Text;
using Xceed.Chart.Core;
using Xceed.Chart.Standard;
using Xceed.Chart;
using Xceed.Chart.GraphicsCore;
namespace Gadgets
{
	/// <summary>
	/// Summary description for ABSWorkItem.
	/// </summary>
	public class ABSCommonLibrary
	{

        public static void SetComboBoxControl(ref ComboBox cboCtrlWnd, MyComboItem[] arrComboItem)
        {
            for (int i = 0; i < arrComboItem.Length; i++)
            {
                cboCtrlWnd.Items.Add(new MyComboItem(arrComboItem[i].ID, arrComboItem[i].Value));
            }
            cboCtrlWnd.DataSource = arrComboItem;
            cboCtrlWnd.ValueMember = "ID";
            cboCtrlWnd.DisplayMember = "Value";
            cboCtrlWnd.SelectedIndex = cboCtrlWnd.Items.Count - 1;
        }

        public static void SetSymbols(ref ComboBox cboCtrlWnd)
        {
            if (DeclareLibrary.SYMBOLS == null)
            {
                object[] p_param = new object[0];
                DeclareLibrary.SYMBOLS = ABSCommonLibrary.DoInquiry(CommonLibrary.CommonLibrary.BOSC010001, p_param);
            }
            DataSet dsData = DeclareLibrary.SYMBOLS.Copy();
            DataRow row = dsData.Tables[0].NewRow();
            row["stock_id"] = -1;
            row["code"] = string.Empty;
            dsData.Tables[0].Rows.Add(row);
            cboCtrlWnd.DataSource = dsData.Tables[0];
            cboCtrlWnd.ValueMember = "stock_id";
            cboCtrlWnd.DisplayMember = "code";
            cboCtrlWnd.SelectedIndex = cboCtrlWnd.Items.Count - 1;
        }
        
        private static bool CheckUrlActive(string url2check)
        {
            string URL = url2check;
            //Create a request for the URL.
            System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
            request.Proxy = null;
            //Get the response.

            try
            {
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                if (response.StatusDescription == "OK")
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
            
        }
        public static void InitChartControl(ref ChartControl p_ChartControl)
        {
            p_ChartControl.Settings.RenderDevice = RenderDevice.GDI;
            p_ChartControl.Background.FillEffect.SetGradient(GradientStyle.Horizontal, GradientVariant.Variant2, Color.Ivory, Color.Khaki);

            // add a header label
            ChartLabel header = p_ChartControl.Labels.AddHeader("hoanvk@gmail.com");
            header.TextProps.Backplane.Visible = false;
            header.TextProps.FillEffect.Color = Color.Navy;
            header.TextProps.Shadow.Type = ShadowType.Solid;
            header.TextProps.HorzAlign = HorzAlign.Left;
            header.TextProps.VertAlign = VertAlign.Top;
            header.HorizontalMargin = 2;
            header.VerticalMargin = 2;

            Chart m_Chart = p_ChartControl.Charts[0];
            m_Chart.View.SetPredefinedProjection(PredefinedProjection.Orthogonal);
            m_Chart.Axis(StandardAxis.Depth).Visible = false;
            m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;
            m_Chart.Axis(StandardAxis.PrimaryX).SetMajorShowAtWall(ChartWallType.Back, true);
            m_Chart.Axis(StandardAxis.PrimaryX).MajorGridLine.Pattern = LinePattern.Dot;
            m_Chart.Axis(StandardAxis.PrimaryY).MajorGridLine.Pattern = LinePattern.Dot;
            m_Chart.Wall(ChartWallType.Back).Width = 0;
            m_Chart.Wall(ChartWallType.Left).Width = 0;
            m_Chart.Wall(ChartWallType.Floor).Width = 0;

        }
        public static Gadgets.BOSCService.Service CreateBoscService()
        {
            BOSCService.Service _orderService = new BOSCService.Service();
            BOSCService.AuthHeader _orderHeader = new BOSCService.AuthHeader();
            _orderHeader.Password = "nimda";
            _orderHeader.Username = "admin";
            _orderService.AuthHeaderValue = _orderHeader;
            _orderService.Timeout = System.Threading.Timeout.Infinite;  
            return _orderService;
        }        

        /// <summary>
        /// BOSC webservice method
        /// </summary>
        /// <param name="business_code"></param>
        /// <param name="p_param"></param>
        /// <returns></returns>
        public static DataSet DoInquiry(string business_code,object[] p_param)
        {
            if (DeclareLibrary.GADGET_SERVICE == null)
                DeclareLibrary.GADGET_SERVICE = CreateBoscService();
            return DeclareLibrary.GADGET_SERVICE.DoInquiry(business_code, p_param);
        }

        /// <summary>
        /// BOSC webservice method
        /// </summary>
        /// <param name="business_code"></param>
        /// <param name="p_param"></param>
        /// <returns></returns>
        public static object DoExecute(string business_code, ref object[] p_param)
        {
            if (DeclareLibrary.GADGET_SERVICE == null)
                DeclareLibrary.GADGET_SERVICE = CreateBoscService();
            return DeclareLibrary.GADGET_SERVICE.DoExecute(business_code, ref p_param);
        }

        public static DataSet GetXmlData()
        {
            if (DeclareLibrary.GADGET_SERVICE == null)
                DeclareLibrary.GADGET_SERVICE = CreateBoscService();
            return DeclareLibrary.GADGET_SERVICE.GetXmlData();
        } 

		public static DialogResult ShowQuestion(string Text)
		{
			return
				MessageBox.Show(Text, "Gadget", MessageBoxButtons.YesNo,
				MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
		}
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sMessage"></param>
		public static void ShowMessage(String sMessage)
		{
			MessageBox.Show(sMessage, "Gadget", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		public static void ShowErrorMessage(String sMessage)
		{
			MessageBox.Show(sMessage, "Gadget", MessageBoxButtons.OK, MessageBoxIcon.Error);			
		}
				
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sToCrypt"></param>
		/// <returns></returns>
        public static string GetMD5HashData(string data)
        {
            //create new instance of md5
            MD5 md5 = MD5.Create();

            //convert the input text to array of bytes
            byte[] hashData = md5.ComputeHash(Encoding.Default.GetBytes(data));

            //create new instance of StringBuilder to save hashed data
            StringBuilder returnValue = new StringBuilder();

            //loop for each byte and add it to StringBuilder
            for (int i = 0; i < hashData.Length; i++)
            {
                returnValue.Append(hashData[i].ToString());
            }

            // return hexadecimal string
            return returnValue.ToString();
        }

        public static string GetSHA1HashData(string data)
        {
            //create new instance of md5
            SHA1 sha1 = SHA1.Create();

            //convert the input text to array of bytes
            byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));

            //create new instance of StringBuilder to save hashed data
            StringBuilder returnValue = new StringBuilder();

            //loop for each byte and add it to StringBuilder
            for (int i = 0; i < hashData.Length; i++)
            {
                returnValue.Append(hashData[i].ToString());
            }

            // return hexadecimal string
            return returnValue.ToString();
        }
		
        public static void CreateGridView(ref Xceed.Grid.GridControl gridControl1)
        {
            DataSet data = new DataSet();
            DataTable table = new DataTable("GridView");
            foreach (Xceed.Grid.Column col in gridControl1.Columns)
            {
                table.Columns.Add(new DataColumn(col.FieldName, col.DataType));
                col.Title = DeclareLibrary.XML_DATA.Tables[gridControl1.DataMember]
                    .Rows[0][col.FieldName].ToString();
            }
            data.Tables.Add(table);
            gridControl1.SetDataBinding(data, "GridView");
        }
	}
}
